//
//  DialogJingjia.swift
//  jiaoyi
//
//  Created by wjk on 2018/11/5.
//  Copyright © 2018 wjk. All rights reserved.
//

import UIKit
import SwiftyJSON


typealias cancleBlock = (([String:String]) -> ())

typealias okBlock = (([String:String]) -> ())

typealias showDialogBlock = (() -> ())


class DialogJingjia: UIView {

    var cb:cancleBlock?
    var ob:okBlock?
    
    @IBOutlet weak var labelAddress: UILabel!
    @IBOutlet weak var labelPrice: UILabel!
    @IBOutlet weak var labelNum: UILabel!
    @IBOutlet weak var labelCate: UILabel!
    @IBOutlet weak var labelNo: UILabel!
    
    @IBOutlet weak var tfPrice: UITextField!
    @IBOutlet weak var tfAddress: UITextField!
    
    @IBOutlet weak var labelUnit: UILabel!
    @IBOutlet weak var textView: UITextView!
    
    static public func showView(frame: CGRect, data:JSON) -> DialogJingjia{
        let v = Bundle.main.loadNibNamed("DialogJingjia", owner: nil, options: nil)?.first as! DialogJingjia
        v.frame = frame
        
        v.labelNo.text = data["alias"].stringValue
        v.labelCate.text = data["variety_name"].stringValue
        v.labelNum.text = "\(data["num"].stringValue) \(data["num_unit_text"].stringValue)"
        v.labelPrice.text = "\(data["base_price"].stringValue) \(data["price_unit_text"].stringValue)"
        v.labelAddress.text = data["depot_name"].stringValue
        v.labelUnit.text = data["price_unit_text"].stringValue
        return v
    }
    
    init(frame: CGRect, data:JSON) {
        super.init(frame: frame)
//        return
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
//        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    @IBAction func ok(_ sender: UIButton) {
        
        if self.tfPrice.text!.isEmpty {
            showMessage(message: "请先输入价格")
            return
        }
        
        ob?(["price":self.tfPrice.text!,"address":self.tfAddress.text!,"note":self.textView.text!])
    }
    
    @IBAction func cancel(_ sender: UIButton) {
        cb?(["":""])
        self.removeFromSuperview()
    }
}
